<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      *{
        padding: 0;
        margin: 0;
        box-sizing: border-box;
      }
      textarea {
        width: 500px;
        border: 1px solid #ccc;
      }
      #dragButton {
        width: 120px;
        height: 40px;
        line-height: 40px;
        background: red;
        text-align: center;
        color: #fff;
        position: fixed;
        left: 0;
        top: 0;
      }
    </style>
  </head>
  <body>
    <div id="dragButton" draggable="true" ondragenter="dragStart(event)">
      拖拽我
    </div>
  </body>
  <script>
    let _offsetY = 0, _offsetX = 0
    function dragStart(event) {
      _offsetY =  event.offsetY
      _offsetX =  event.offsetX
    }
    document.addEventListener("dragover", function (event) {
      event.preventDefault();
    });
    document.addEventListener("drop", function (event) {
      let _left = event.clientX - _offsetX
      let _top = event.clientY - _offsetY
      dragButton.style.left = _left + "px";
      dragButton.style.top = _top + "px";
    });
  </script>
</html>
